package com.xmall.auth.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.auth.entity.SysResource;
import com.xmall.auth.service.ISysResourceService;
import com.xmall.common.config.CommonPage;
import com.xmall.common.config.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author liuzh
 * @since 2020-06-14
 */
@RestController
@RequestMapping("/resource")
public class SysResourceController {
    @Autowired
    private ISysResourceService sysResourceService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult registerMenu(@RequestBody SysResource sysResource) {
            boolean save = sysResourceService.saveSysResource( sysResource );
        if(!save){
            return CommonResult.failed();
        }
        return CommonResult.success(save);
    }


    @ApiOperation("删除后台资源表")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {
            boolean delete = sysResourceService.deleteSysResource(id);
        if(!delete){
            return CommonResult.failed();
        }
        return CommonResult.success(delete);
    }

    @ApiOperation("更新后台资源表")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SysResource sysResource ) {
        boolean update = sysResourceService.updateSysResource(id,sysResource);
        if(!update){
            return CommonResult.failed();
        }
            return CommonResult.success(update);
    }

    @ApiOperation("获取后台资源表ById")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SysResource> getSysResource(@PathVariable Long id) {
    SysResource sysResource = sysResourceService.getSysResource(id);
            return CommonResult.success(sysResource);
    }



    @ApiOperation("分页查询后台资源表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysResource>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        Page<SysResource> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysResource> queryWrapper = new QueryWrapper<>();
        if(!StrUtil.isEmpty(keyword)){
            queryWrapper.like("name",keyword);
        }
        IPage<SysResource> pageList = sysResourceService.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(pageList));
    }
}

